package main

import (
	"flag"
	"net/http"

	"github.com/golang/glog"
)

var addr = flag.String("addr", ":8082", "http service address")

func serveHome(w http.ResponseWriter, r *http.Request) {
	glog.Info(r.URL)
	if r.URL.Path != "/" {
		http.Error(w, "Not found", http.StatusNotFound)
		return
	}

	if r.Method != http.MethodGet {
		http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
		return
	}

	http.ServeFile(w, r, "../ui/src/index.html")
}

func main() {
	flag.Parse()
	glog.Infof("start at %v", *addr)
	hub := NewHub()
	go hub.run()

	http.HandleFunc("/", serveHome)
	http.HandleFunc("/signaling", func(w http.ResponseWriter, r *http.Request) {
		glog.Info("a client enter...")
		serveWs(hub, w, r)
	})

	if err := http.ListenAndServe(*addr, nil); err != nil {
		glog.Fatal("ListenAdnServe: ", err)
	}
}
